package com.ice.body.hardware.controller;
import com.ice.body.business.model.WareHouseModel;
import com.ice.body.business.service.WareHouseService;
import com.ice.body.hardware.model.DeviceWeigh;
import com.ice.body.hardware.service.DeviceWeighService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import com.ice.base.controller.BaseController;
import com.github.pagehelper.PageHelper;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import com.ice.base.result.ResponseResult;
import com.ice.base.result.AjaxResult;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
/**
 * Created with GuoYanBin.
 * Description:
 * Date: 2021-12-16
 * Time: 09:44
 */
@RestController
@ApiIgnore(value = "称重")
public class DeviceWeighController extends BaseController{
    //---------------------------称重-----------------------start
    @Resource
    private DeviceWeighService deviceWeighService;
    @Resource
    private WareHouseService wareHouseService;

    @ApiOperation(value = "称重-新增")
    @PostMapping("/api/DeviceWeigh/insert")
    public AjaxResult<DeviceWeigh> insert_DeviceWeigh(DeviceWeigh model) {
        AjaxResult r = ResponseResult.success();
        try{
            WareHouseModel wareHouseModel = wareHouseService.selectById(model.getWarehouseId().toString());

            model.setCreateTime(new Date());
            model.setWarehouseName(wareHouseModel.getWarehouseName());
            deviceWeighService.insert(model);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "称重-删除")
    @PostMapping("/api/DeviceWeigh/deleteById")
    public AjaxResult<DeviceWeigh> deleteById_DeviceWeigh(@RequestParam("id") String id) {
        AjaxResult r = ResponseResult.success();
        try{
            Integer count = deviceWeighService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "称重-更新")
    @PostMapping("/api/DeviceWeigh/updateById")
    public AjaxResult<DeviceWeigh> update_DeviceWeigh(DeviceWeigh model) {
        AjaxResult r = ResponseResult.success();
        try{
            Integer count = deviceWeighService.update(model);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "称重-查询一个")
    @PostMapping("/api/DeviceWeigh/selectById")
    public AjaxResult<DeviceWeigh> selectById_DeviceWeigh(@RequestParam("id") String id) {
        AjaxResult r = ResponseResult.success();
        try{
           DeviceWeigh model = deviceWeighService.selectById(id);
           r.setData(model);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "称重-查询多个")
    @PostMapping("/api/DeviceWeigh/selectAll")
    public AjaxResult<DeviceWeigh> selectList_DeviceWeigh(DeviceWeigh model) {
        AjaxResult r = ResponseResult.success();
        try{
           List<DeviceWeigh> list = deviceWeighService.selectListByModel(model);
           r.setData(list);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "称重-查询分页")
    @PostMapping("/api/DeviceWeigh/selectListByPageHelper")
    public AjaxResult<DeviceWeigh> selectListByPageHelper_DeviceWeigh(@RequestParam(name = "pageSize") Integer pageSize,
                                                                      @RequestParam(name = "pageNum") Integer pageNum,
                                                                      DeviceWeigh model,
                                                                      String keyWord){
        AjaxResult r = ResponseResult.success();
        ArrayList list;
        try{
           PageHelper.startPage(pageNum, pageSize);
           if(StringUtils.isEmpty(keyWord)) {
               list = deviceWeighService.selectListByModel(model);
           } else {
               list = deviceWeighService.selectListByKeyWord(keyWord);
           }
           getReturnResultByPage(r,list);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }
    //---------------------------称重-----------------------end
}
